﻿﻿
package com.ufenlei.LTS {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;

	public class Window extends Sprite {
		private var father : Sprite;
		private var winZoomButton : ZoomButton;
		private var winCloseButton : CloseButton;
		private var _isMax : Boolean;
		private var isOpened : Boolean;
		private var winBody : RoundRectBody;
		private var winHead : RoundRectHead;
		private var _content : Array = new Array();
		private var _winWidth:int;
		private var _winHeight:int;

		public var warningString : String = "确定要关闭吗";
		public var closeMode : Boolean;

		public function Window(fa : Sprite,
		                       winW : int = 0,
		                       winH : int = 0,
		                       title : String = null,
		                       winVisible : Boolean = false,
		                       isShadow : Boolean = true,
		                       closeM : Boolean = true,
		                       body_alpha:Number = 0.5) {              	
			closeMode = closeM;
			_isMax = true;
			father = fa;
			winBody = new RoundRectBody(winW, winH - 20, 5, 5, isShadow,body_alpha);
			winHead = new RoundRectHead(winW, 25, 5, 5, title);
			winZoomButton = new ZoomButton;
			winCloseButton = new CloseButton;
			winZoomButton.y = winZoomButton.height / 5;
			winCloseButton.y = winCloseButton.height / 5;
			winZoomButton.x = winBody.width - winZoomButton.width - winCloseButton.width - 2 * winCloseButton.width / 5 - 1;
			winCloseButton.x = winHead.width - winCloseButton.width - winCloseButton.width / 5;
			winBody.y += winHead.height - 2;
			this.addEventListener(MouseEvent.MOUSE_DOWN, goTop);
			father.addEventListener(MouseEvent.CLICK, stopD);
			winZoomButton.addEventListener("Input_MOUSEUP", zoomWindow);
			winCloseButton.addEventListener(Input.CLICK, closeWindow);
			winHead.addEventListener(MouseEvent.MOUSE_DOWN, goWithMouse);
			winHead.addEventListener(MouseEvent.MOUSE_UP, stopGoWithMouse);
			this.addChild(winHead);
			this.addChild(winBody);
			this.addChild(winZoomButton);
			this.addChild(winCloseButton);
			if(winVisible) {
				isOpened = true;
				father.addChild(this);
			}
			_winHeight = winH+3;
			_winWidth = winW;
		}
		
		public function get winHeight():int{
			return _winHeight;
		}
		public function get winWidth():int{
			return _winWidth;
		}
		public function setTitle(title : String) : void {
			winHead.setTitle(title);
		}

		
		public function get isMax() : Boolean {
			return this._isMax;
		}

		public function maximize() : void {
			addChild(winBody);
			_isMax = true;
		}

		public function minimize() : void {
			removeChild(winBody);
			_isMax = false;
		}

		public function addContent(child : Sprite) : void {
			winBody.addContent(child);
		}

		public function clearContent(child : Sprite) : void {
			winBody.removeContent(child);
		}

		public function clearAllContent() : void {
			winBody.removeAllContent();
		}

		public function get isopen() : Boolean {
			return this.isOpened;
		}

		public function open() : void {
			freeze();
			var blackshadow : BlackShadow;
			if (_isMax == true) {
				if (isOpened == false) {
					blackshadow = new BlackShadow(father, winHead.headWidth, winHead.headHeight + winBody.bodyHeight, "open");
					blackshadow.x = this.x + winHead.headWidth / 2;
					blackshadow.y = this.y;
					blackshadow.addEventListener("Shadow_OPEN", openWindow);
					isOpened = true;
				}
			} else {
				if (isOpened == false) {
					blackshadow = new BlackShadow(father, winHead.headWidth, winHead.headHeight, "open");
					blackshadow.x = this.x + winHead.headWidth / 2;
					blackshadow.y = this.y;
					blackshadow.addEventListener("Shadow_OPEN", openWindow);
					isOpened = true;
				}
			}
		}

		public function close() : void {
			var blackshadow : BlackShadow;
			if (_isMax == true) {
				if (isOpened == true) {
					blackshadow = new BlackShadow(father, winHead.headWidth, winHead.headHeight + winBody.bodyHeight, "close");
					blackshadow.x = this.x + winHead.headWidth / 2;
					blackshadow.y = this.y + winHead.headHeight + winBody.bodyHeight / 2;
					isOpened = false;
					blackshadow.addEventListener("Shadow_CLOSE", closeEvent);
					father.removeChild(this);
				}
			} else {
				if (isOpened == true) {
					blackshadow = new BlackShadow(father, winHead.headWidth, winHead.headHeight, "close");
					blackshadow.x = this.x + winHead.headWidth / 2;
					blackshadow.y = this.y + winHead.headHeight / 2;
					blackshadow.addEventListener("Shadow_CLOSE", closeEvent);
					isOpened = false;
					father.removeChild(this);
				}
			}
		}

		private function zoomWindow(evt : Event) : void {
			if (_isMax == true) {
				minimize();
			} else {
				maximize();
			}
		}

		private function closeWindow(evt : Event) : void {
			if(closeMode) {
				var messagewin : MessageWindow = new MessageWindow(this, "警告", warningString, false, false);
				messagewin.x = winHead.headWidth / 2 - messagewin.width / 2;
				messagewin.y = winBody.bodyHeight / 2 - messagewin.height / 2;
				messagewin.open();
				lock();
				messagewin.addEventListener(Event.CLOSE, closeThis);
				messagewin.addEventListener(Event.CANCEL, free);			
			} else {
				close();
			}
		}

		private function closeThis(evt : Event) : void {
			close();
		}

		private function free(evt : Event) : void {
			freeze();
		}

		private function openWindow(evt : Event) : void {
			father.addChild(this);
			this.dispatchEvent(new Event(Event.OPEN));
		}

		private function goTop(evt : MouseEvent) : void {
			father.setChildIndex(this, father.numChildren - 1);
		}

		private function goWithMouse(evt : MouseEvent) : void {
			winBody.alpha = 0.5;
			this.startDrag();
		}

		private function stopGoWithMouse(evt : MouseEvent) : void {
			winBody.alpha = 1;
			this.stopDrag();
		}

		private function fliter() : void {
			var shadow : DropShadowFilter = new DropShadowFilter(4, 60, 0, 1.0, 20, 20, 1.0, 3);
			this.filters = [shadow];
			var filtersArray : Array = new Array(shadow);
			this.filters = filtersArray;
		}

		private function stopD(evt : MouseEvent) : void {
			this.stopDrag();
		}

		private function lock() : void {
			this.removeEventListener(MouseEvent.MOUSE_DOWN, goTop);
			father.removeEventListener(MouseEvent.CLICK, stopD);
			winZoomButton.removeEventListener("Input_MOUSEUP", zoomWindow);
			winCloseButton.removeEventListener(Input.CLICK, closeWindow);
			winHead.removeEventListener(MouseEvent.MOUSE_DOWN, goWithMouse);
			winHead.removeEventListener(MouseEvent.MOUSE_UP, stopGoWithMouse);
			winZoomButton.lock();
			winCloseButton.lock();
		}

		private function freeze() : void {
			this.addEventListener(MouseEvent.MOUSE_DOWN, goTop);
			father.addEventListener(MouseEvent.CLICK, stopD);
			winZoomButton.addEventListener("Input_MOUSEUP", zoomWindow);
			winCloseButton.addEventListener(Input.CLICK, closeWindow);
			winHead.addEventListener(MouseEvent.MOUSE_DOWN, goWithMouse);
			winHead.addEventListener(MouseEvent.MOUSE_UP, stopGoWithMouse);
			winZoomButton.unlock();
			winCloseButton.unlock();
		}

		private function closeEvent(evt : Event) : void {
			this.dispatchEvent(new Event(Event.CLOSE));	
		}
	}
}